package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.HealthmanagerDao;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryData;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.HealthManager;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private HealthmanagerDao healthmanagerDao;

    @Override
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findByTelephone(telephone);
        return member;
    }

    @Override
    public void add(Member member) {
        int h_id = findIdByName(member);
        try {
            memberDao.add(member);
            memberDao.insertMemberHealthManager(member.getId(),h_id);
            System.out.println(1111);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> list) {
        List<Integer> counts = new ArrayList<>();
        for (String s : list) {
//            s+="-31";
            Integer count = memberDao.findMemberCountBeforeDate(s);
            counts.add(count);
        }
        return counts;
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        QueryData queryData = queryPageBean.getQueryData();
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = memberDao.selectByCondition(queryData);
        PageResult pr = new PageResult(page.getTotal(), page.getResult());
        return pr;
    }

    @Override
    public Member findById(int id) {
        Member member = memberDao.findById(id);
        return member;
    }

    @Override
    public void edit(Member member) {
        //修改会员表
        if(member!=null && member.getH_name()!=null){
            int h_id = findIdByName(member);
            int memberId = member.getId();
            //如果有就修改 注意 这地方前台修改了健康管理师 名字虽然变了 但是刚才查询的id并没有改变 所以应该用name去health_manager表中去查询id
            //先删除
            memberDao.deleteMemberHealthManagerById(memberId);
            //新增
            memberDao.insertMemberHealthManager(memberId,h_id);
        }
        memberDao.edit(member);
    }

    @Override
    public boolean deletes(Integer[] id) {
        StringBuffer buf = new StringBuffer();
        for (Integer i : id) {
            buf.append(i + ",");
        }
        if (buf.length() > 0) {
            String str = buf.substring(0, buf.length() - 1);
            memberDao.deletesMid(str);
            memberDao.deletes(str);
            return true;
        }
        return false;
    }

    @Override
    public List<Member> findAll() {
        List<Member> list = memberDao.findAll();
        return list;
    }

    @Override
    public List<HealthManager> findAllHealthManager() {
        List<HealthManager> all = healthmanagerDao.findAll();
        return all;
    }

    @Override
    public int findMidByMemberId(Integer[] id) {
        StringBuffer buf = new StringBuffer();
        for (Integer i : id) {
            buf.append(i + ",");
        }
        int count=0;
        if (buf.length() > 0) {
            String str = buf.substring(0, buf.length() - 1);
             count = memberDao.findMidByMemberId(str);
        }
        return count;
    }

    public int findIdByName(Member member){
        int h_id=0;
        String h_name = member.getH_name();
        if(h_name!=null){
            //根据健康管理师名字查询id
            h_id = memberDao.findIdByName(h_name);
        }
        return h_id;
    }
}
